package lleida.festamajor.controllers;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;

import com.google.android.maps.GeoPoint;

import lleida.festamajor.model.Category;
import lleida.festamajor.model.Date;
import lleida.festamajor.model.Event;
import lleida.festamajor.model.Place;

public class EventController {
	private static ArrayList<Event> events = new ArrayList<Event>();
	private static ArrayList<Place> places = new ArrayList<Place>();
	private static ArrayList<Date> dates = new ArrayList<Date>();
	private static ArrayList<Category> categories = new ArrayList<Category>();
	
	private static ArrayList<Event> activeEvents = null;
	private static int selectedEvent;
	private static ArrayList<GeoPoint> activePlaces = null;
	private static ArrayList<String> activePlacesTitles = null;
	
	public static int init() {
		Calendar c = Calendar.getInstance();
		SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy/MM/dd");
		SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm");
		
		String time = sdfTime.format(c.getTime());
		String day = "";
		if (time.compareTo("06:00") >= 0)
			day = sdfDay.format(c.getTime());
		else {
			c.add(Calendar.DAY_OF_YEAR, -1);
			day = sdfDay.format(c.getTime());
		}
		
		int pos = 0;
		for (; pos < dates.size(); ++pos)
			if (dates.get(pos).getFormated().compareTo(day) >= 0) break;
		if (pos == dates.size()) pos--;
		setActiveDate(pos);
		
		return pos;
		
		/*
		if (dates.size() > 0){
			activeEvents = dates.get(0).getEvents();
			Collections.sort(activeEvents);
		}
		*/
	}
	
	public static void readDates(InputStream input) {
		dates = XMLReader.readDates(input);
	}
	
	public static void readPlaces(InputStream input) {
		places = XMLReader.readPlaces(input);
	}
	
	public static void readCategories(InputStream input) {
		categories = XMLReader.readCategories(input);
	}
	
	public static void readEvents(InputStream input) {
		events = XMLReader.readEvents(input);
		for (Event e : events) {
			e.getDate().addEvent(e);
			e.getPlace().addEvent(e);
		}
	}
	
	public static int getTotalEvents() {
		return events.size();
	}
	
	public static int getNumEvents() {
		return activeEvents.size();
	}
	
	public static String getEventName(int i) {
		return activeEvents.get(i).getName();
	}
	
	public static String getEventPlace(int i) {
		return activeEvents.get(i).getPlace().getName();
	}
	
	public static String getEventTime(int i) {
		return activeEvents.get(i).getTime();
	}
	
	public static int getEventCategoryIcon(int i) {
		return activeEvents.get(i).getCategory().getIcon_id();
	}
	
	public static boolean getEventFree(int i) {
		return activeEvents.get(i).isFree();
	}
	
	public static void setSelectedEvent(int i) {
		selectedEvent = i;
	}
	
	public static void setSelectedNext() {
		selectedEvent++;
		if (selectedEvent >= activeEvents.size()) selectedEvent = activeEvents.size() - 1;
	}
	
	public static void setSelectedPrevious() {
		selectedEvent--;
		if (selectedEvent < 0) selectedEvent = 0;
	}
	
	public static String getSelectedEventName() {
		return activeEvents.get(selectedEvent).getName();
	}
	
	public static String getSelectedEventPlace() {
		return activeEvents.get(selectedEvent).getPlace().getName();
	}
	
	public static String getSelectedEventTime() {
		return activeEvents.get(selectedEvent).getTime();
	}
	
	public static String getSelectedEventDate() {
		return activeEvents.get(selectedEvent).getDate().getDate();
	}
	
	public static String getSelectedEventDescription() {
		return activeEvents.get(selectedEvent).getDescription();
	}
	
	public static int getSelectedEventCategoryIcon() {
		return activeEvents.get(selectedEvent).getCategory().getIcon_id();
	}
	
	public static int getPositionByTime(String time) {
		int pos = 0;
		
		for (;pos < activeEvents.size(); ++pos) {
			if (time.compareTo("06:00") >= 0 || activeEvents.get(pos).getTime().compareTo("06:00") < 0)
			if (activeEvents.get(pos).getTime().compareTo(time) >= 0) return pos;
		}
		
		return pos - 1;
	}
	
	public static void setActiveDate(int i) {
		if (dates.size() > i) {
			activeEvents = dates.get(i).getEvents();
			Collections.sort(activeEvents);
		}
	}
	
	public static ArrayList<String> getDates() {
		ArrayList<String> ret = new ArrayList<String>();
		for (Date d : dates)
			ret.add(d.getDate());
		return ret;
	}
	
	public static ArrayList<String> getNextEventsName() {
		ArrayList<String> list = new ArrayList<String>();
		for (int i = 0; i < activeEvents.size(); ++i)
			list.add(activeEvents.get(i).getName());
		return list;
	}
	
	public static ArrayList<String> getNextEventsTime() {
		ArrayList<String> list = new ArrayList<String>();
		for (int i = 0; i < activeEvents.size(); ++i)
			list.add(activeEvents.get(i).getTime());
		return list;
	}
	
	public static ArrayList<String> getNextEventsPlace() {
		ArrayList<String> list = new ArrayList<String>();
		for (int i = 0; i < activeEvents.size(); ++i)
			list.add(activeEvents.get(i).getPlace().getName());
		return list;
	}
	
	public static Category getCategory(int i) {
		return categories.get(i);
	}
	
	public static Place getPlace(int i) {
		return places.get(i);
	}
	
	public static Date getDate(int i) {
		return dates.get(i);
	}
	
	public static void setAllSelectedPlaces() {
		ArrayList<GeoPoint> tempPoints = new ArrayList<GeoPoint>();
		ArrayList<String> tempTitles = new ArrayList<String>();
		for (Place p : places)
			setPlace(tempPoints, tempTitles, p);
		activePlaces = tempPoints;
		activePlacesTitles = tempTitles;
	}
	
	public static void setSelectedPlace() {
		ArrayList<GeoPoint> tempPoints = new ArrayList<GeoPoint>();
		ArrayList<String> tempsTitles = new ArrayList<String>();
		Place p = activeEvents.get(selectedEvent).getPlace();
		setPlace(tempPoints, tempsTitles, p);
		activePlaces = tempPoints;
		activePlacesTitles = tempsTitles;
	}
	
	private static void setPlace(ArrayList<GeoPoint> listPoints, ArrayList<String> listTitles, Place p) {
		listPoints.add(new GeoPoint((int)(p.getLat()*1E6), (int)(p.getLon()*1E6)));
		listTitles.add(p.getName());
	}
	
	public static ArrayList<GeoPoint> getSelectedPlaces() {
		if (activePlaces != null) return activePlaces;
		return new ArrayList<GeoPoint>();
	}
	
	public static ArrayList<String> getSelectedPlacesTitle() {
		if (activePlacesTitles != null) return activePlacesTitles;
		return new ArrayList<String>();
	}
}
